# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import time

from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
from django.core.serializers import json
from django.http import HttpResponse
from django.shortcuts import render
import os
from CaseApp import models


def index(request):
    if request.method == "GET":
        AllCase = models.CaseFactory.objects.all()
        paginator = Paginator(AllCase, 10)
        page = request.GET.get('page')
        try:
            contacts = paginator.page(page)  # contacts为Page对象！
        except PageNotAnInteger:
            contacts = paginator.page(1)
        except EmptyPage:
            contacts = paginator.page(paginator.num_pages)

    return render(request, "index.html", {'contacts': contacts})
def detail(request):
    if request.method == "GET":
        SearchID = request.GET.get('BtE', None)
        OneCase = models.CaseFactory.objects.filter(id= SearchID)
        print(OneCase.values('FileName'))

    return render(request, "detail.html", {'OneCase': OneCase})

def GetCaseByID(request):
    if request.method == "GET":
        SearchID = request.GET.get('SearchID')
        SearchPerson = request.GET.get('SearchPerson')
        AllCase = models.CaseFactory.objects.all()
        if SearchID != " ":
            AllCase = models.CaseFactory.objects.filter(id=SearchID)
        print(AllCase)
        paginator = Paginator(AllCase, 10)
        page = request.GET.get('page')
        try:
            contacts = paginator.page(page)  # contacts为Page对象！
        except PageNotAnInteger:
            contacts = paginator.page(1)
        except EmptyPage:
            contacts = paginator.page(paginator.num_pages)

    return render(request, "index.html", {'contacts': contacts})
def GetCaseByPerson(request):
    if request.method == "GET":
        SearchPerson = request.GET.get('SearchPerson')
        AllCase = models.CaseFactory.objects.all()
        if SearchPerson != " ":
            AllCase = models.CaseFactory.objects.filter(PersonName__contains=SearchPerson[1:])
        paginator = Paginator(AllCase, 10)
        page = request.GET.get('page')
        try:
            contacts = paginator.page(page)  # contacts为Page对象！
        except PageNotAnInteger:
            contacts = paginator.page(1)
        except EmptyPage:
            contacts = paginator.page(paginator.num_pages)

    return render(request, "index.html", {'contacts': contacts})

def GetCaseByProject(request):
    if request.method == "GET":
        SearchProject = request.GET.get('SearchProject')
        AllCase = models.CaseFactory.objects.all()
        if SearchProject != " ":
            AllCase = models.CaseFactory.objects.filter(ProjectName__contains=SearchProject[1:])
        paginator = Paginator(AllCase, 10)
        page = request.GET.get('page')
        try:
            contacts = paginator.page(page)  # contacts为Page对象！
        except PageNotAnInteger:
            contacts = paginator.page(1)
        except EmptyPage:
            contacts = paginator.page(paginator.num_pages)

    return render(request, "index.html", {'contacts': contacts})

def uploadFile(request):
    if request.method == "POST":
        myFile = request.FILES.get('myfile', None)
        ProjectName = request.POST.get('ProjectName',None)
        Status = request.POST.get('Status',None)
        FileContext = request.POST.get('FileContext', None)
        PersonName = request.POST.get('PersonName', None)
        SITE_ROOT = os.path.join(os.path.abspath(os.path.dirname(__file__)))
        filepath = os.path.join(os.path.join(SITE_ROOT, 'static\\file\\') + str(int(time.time())) + '_' + myFile.name)
        print(filepath)
        with open(filepath, 'wb+') as f:
            for chunk in myFile.chunks():
                f.write(chunk)
                f.close()
                dic = {"FileName": str(int(time.time())) + '_' + myFile.name,
                       "Status": Status, "ProjectName" : ProjectName,
                       "FileContext": FileContext, "PersonName" : PersonName,
                       "CreateTime": time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))}
                models.CaseFactory.objects.create(**dic)
        return render(request, "index.html", {'contacts': ""})
    AllCase = models.CaseFactory.objects.all()
    paginator = Paginator(AllCase, 10)
    page = request.GET.get('page')
    try:
        contacts = paginator.page(page)  # contacts为Page对象！
    except PageNotAnInteger:
        contacts = paginator.page(1)
    except EmptyPage:
        contacts = paginator.page(paginator.num_pages)
    return render(request, "index.html", {'contacts': contacts})

def Add(request):
    return render(request,"AddCase.html",{'':''})

def Del(request):
    if request.method == "POST":
        id = request.GET.get('id', None)
        models.CaseFactory.objects.filter(id=id).delete()
        return render(request, "index.html", {'result': "成功"})
    return render(request, "index.html", {'result': "失败"})

def Update(request):
    AllCase = models.CaseFactory.objects.all()
    paginator = Paginator(AllCase, 10)
    page = request.GET.get('page')
    try:
        contacts = paginator.page(page)  # contacts为Page对象！
    except PageNotAnInteger:
        contacts = paginator.page(1)
    except EmptyPage:
        contacts = paginator.page(paginator.num_pages)

    if request.method == "GET":
        id = request.GET.get('id', None)
        FileContext = request.GET.get('FileContext', None)
        ProjectName = request.GET.get('ProjectName', None)
        Status = request.GET.get('Status', None)
        models.CaseFactory.objects.filter(id=id).update(FileContext=FileContext, ProjectName=ProjectName, Status=Status)
    return render(request, "index.html", {'contacts': contacts})

import jenkins
def APIjenkins(request):
    url = 'http://192.168.162.120:8080'
    username = 'root'
    userpassword = 'test123'
    job_name ='jmeter_test0602'
    server = jenkins.Jenkins(url, username=username, password=userpassword)
    server.build_job(job_name, parameters={"build":"jenkins"}, token="0f2be1174a2e695c8ab061c74b33d417")

    server.get_job_info(job_name)
    return render(request, "index.html", {'result': "失败"})




